/**
 * 
 */
package edu.umd.clip.lm.nbest;

import java.text.ParseException;
import java.util.regex.*;
/**
 * @author Denis Filimonov <den@cs.umd.edu>
 *
 */
public class SRINbestFormat extends NbestFormat {
	private static final Pattern pattern = Pattern.compile("^(-?[.0-9]+)\\s+(-?[.0-9]+)\\s+([0-9]+)\\s+(.*)$");
	
	public static class SRINbestFormatData extends NbestFormat.NbestFormatData {
		protected double amScore;
		protected int length;
		public SRINbestFormatData(double amScore, double lmScore, int length, String sentence) {
			super(lmScore, sentence);
			this.amScore = amScore;
			this.length = length;
		}
		public double getAmScore() {
			return amScore;
		}
		public void setAmScore(double amScore) {
			this.amScore = amScore;
		}
		public int getLength() {
			return length;
		}
		public void setLength(int length) {
			this.length = length;
		}
	}

	/* (non-Javadoc)
	 * @see edu.umd.clip.lm.model.NbestFormat#parse(java.lang.String)
	 */
	@Override
	public NbestFormatData parse(String input) throws ParseException {
		Matcher matcher = pattern.matcher(input);
		if (matcher.matches()) {
			double amScore = Double.parseDouble(matcher.group(1));
			double lmScore = Double.parseDouble(matcher.group(2));
			int length = Integer.parseInt(matcher.group(3));
			String sentence = matcher.group(4);
			return new SRINbestFormatData(amScore, lmScore, length, sentence);
		}
		throw new ParseException("Failed to parse \""+input+"\"", matcher.end());
	}

	/* (non-Javadoc)
	 * @see edu.umd.clip.lm.model.NbestFormat#toString()
	 */
	@Override
	public String toString(NbestFormatData data) {
		if (data instanceof SRINbestFormatData) {
		SRINbestFormatData dat = (SRINbestFormatData) data;
		
		StringBuilder sb = new StringBuilder();
		sb.append(dat.amScore);
		sb.append(' ');
		sb.append(dat.lmScore);
		sb.append(' ');
		sb.append(dat.length);
		sb.append(' ');
		sb.append(dat.sentence);
		sb.append('\n');
		return sb.toString();
		}
		throw new Error("wrong data format");
	}

}
